package com.java2007.zhangyu.restaurant.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.java2007.zhangyu.restaurant.dao.FoodDao;
import com.java2007.zhangyu.restaurant.dao.FoodTypeDao;
import com.java2007.zhangyu.restaurant.dao.impl.FoodDaoImpl;
import com.java2007.zhangyu.restaurant.dao.impl.FoodTypeDaoImpl;
import com.java2007.zhangyu.restaurant.pojo.DinnerTable;
import com.java2007.zhangyu.restaurant.pojo.Food;
import com.java2007.zhangyu.restaurant.pojo.FoodType;
import com.java2007.zhangyu.restaurant.service.FoodTypeService;
import com.java2007.zhangyu.restaurant.utils.JdbcUtils;


import java.sql.SQLException;
import java.util.List;

/**
 * @author zy
 * @Version
 * @date 2021/01/23
 */
public class FoodTypeServiceImpl implements FoodTypeService {
    private FoodTypeDao foodTypeDao = new FoodTypeDaoImpl();
    private FoodDao foodDao = new FoodDaoImpl();

    @Override
    public int saveByFoodTypeName(String fTypeName) throws SQLException {
        try {
            //开启事务
            JdbcUtils.begin();
            foodTypeDao.saveByFoodTypeName(fTypeName);
            //提交事务
            JdbcUtils.commitAndClose();
        } catch (SQLException e) {
            e.printStackTrace();
            //回滚事务
            JdbcUtils.rollbackAndClose();
            return -1;
        }
        return 1;
    }

    @Override
    public int deleteByFoodTypeId(Integer fTypeID) throws SQLException {
        try {
            //开启事务
            JdbcUtils.begin();
            foodTypeDao.deleteByFoodTypeId(fTypeID);
            //提交事务
            JdbcUtils.commitAndClose();
        } catch (SQLException e) {
            e.printStackTrace();
            //回滚事务
            JdbcUtils.rollbackAndClose();
            return -1;
        }
        return 1;
    }

    @Override
    public int updateByFoodTypeName(FoodType foodType) throws SQLException {
        try {
            //开启事务
            JdbcUtils.begin();
            foodTypeDao.updateByFoodTypeName(foodType);
            //提交事务
            JdbcUtils.commitAndClose();
        } catch (SQLException e) {
            e.printStackTrace();
            //回滚事务
            JdbcUtils.rollbackAndClose();
            return -1;
        }
        return 1;
    }

    @Override
    public FoodType queryByFoodTypeId(Integer fTypeId) throws SQLException {
        return foodTypeDao.queryByFoodTypeId(fTypeId);
    }

    @Override
    public List<FoodType> queryByFoodTypeName(String fTypeName) throws SQLException {
        //判断搜索文本框中的状态
        //判断搜索文本框中是否为空
        if(StringUtils.isEmpty(fTypeName)){
            fTypeName = "";
        } else {
            //trim() 方法用于删除字符串的头尾空白符（空白符包括：空格、制表符 tab、换行符等其他空白符等）
            fTypeName = fTypeName.trim();
        }

        //此时，搜索文本框中一定有值 值为 "" 或 川菜
        return foodTypeDao.queryByFoodTypeName("%" + fTypeName + "%");
    }

    @Override
    public List<FoodType> queryAll() throws Exception {
        return foodTypeDao.queryAll();
    }

    @Override
    public int checkFoodTypeName(String fTypeName) {
        int result=0;
        try {
            JdbcUtils.begin();
            FoodType foodType = foodTypeDao.selectFoodTypeByDtName(fTypeName);
            if(foodType !=null){
                result=1;
            }
            JdbcUtils.commitAndClose();
        }catch (Exception e){
            JdbcUtils.rollbackAndClose();
            e.printStackTrace();
        }
        return result;
    }
}
